#!/usr/bin/env bash

source ./config/env.sh
clear

# mkdir -p ${K8S_DATA}/config/docker

# cat > ${K8S_DATA}/config/docker/docker.service <<EOF
# [Unit]
# Description=Docker Application Container Engine
# Documentation=http://docs.docker.io

# [Service]
# WorkingDirectory=${K8S_DATA}/data/docker
# Environment="PATH=${K8S_BIN_DIR}:/bin:/sbin:/usr/bin:/usr/sbin"
# EnvironmentFile=-/run/flannel/docker
# ExecStart=${K8S_BIN_DIR}/dockerd --log-level=error \$DOCKER_NETWORK_OPTIONS
# ExecReload=/bin/kill -s HUP \$MAINPID
# Restart=on-failure
# RestartSec=5
# LimitNOFILE=infinity
# LimitNPROC=infinity
# LimitCORE=infinity
# Delegate=yes
# KillMode=process

# [Install]
# WantedBy=multi-user.target
# EOF


# cat > ${K8S_DATA}/config/docker/docker-daemon.json <<EOF
# {
#     "registry-mirrors": ["https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],
#     "insecure-registries": ["192.168.74.130","192.168.74.131"],
#     "max-concurrent-downloads": 20,
#     "live-restore": true,
#     "max-concurrent-uploads": 10,
#     "debug": true,
#     "data-root": "${K8S_DATA}/data/docker/data",
#     "exec-root": "${K8S_DATA}/data/docker/exec",
#     "log-opts": {
#       "max-size": "100m",
#       "max-file": "5"
#     }
# }
# EOF

cd ${K8S_DATA}/config/docker

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    scp ${K8S_DATA}/config/docker/docker.service root@${node_ip}:/etc/systemd/system/
    ssh root@${node_ip} "mkdir -p ${K8S_DATA}/data/docker/{data,exec}"
    ssh root@${node_ip} "test -e /etc/docker || mkdir -p /etc/docker"
    scp ${K8S_DATA}/config/docker/docker-daemon.json root@${node_ip}:/etc/docker/daemon.json
done

for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "systemctl stop firewalld && systemctl disable firewalld"
    ssh root@${node_ip} "/usr/sbin/iptables -F && /usr/sbin/iptables -X && /usr/sbin/iptables -F -t nat && /usr/sbin/iptables -X -t nat"
    ssh root@${node_ip} "/usr/sbin/iptables -P FORWARD ACCEPT"
    ssh root@${node_ip} "systemctl daemon-reload && systemctl enable docker && systemctl restart docker"
    ssh root@${node_ip} "sudo sysctl -p /etc/sysctl.d/kubernetes.conf"
done


for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "systemctl status docker|grep Active"
done


for node_ip in ${NODE_IP}
do
    echo "=========================================== ${node_ip} ==========================================="
    ssh root@${node_ip} "/usr/sbin/ip addr show flannel1.1 && /usr/sbin/ip addr show docker0"
done

